package model.objects;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Random;

import javax.swing.JPanel;

import model.general.Location;
import model.move.IMove;
import model.move.MoveItems;
import model.observer.Obsever;
import model.observer.Obseverable;
import model.option.Option;

public class Fish extends Sprites implements IAnimateItems {
	private Option option;
	private IAnimateSubActor hook;
	private IMove move;
	private JPanel pnl;
	
	private boolean isCatch = false;
	private boolean isMove = true;
	private boolean isStandLeft;
	private boolean isChecking = true;
	
	public Fish(int x, int y, String srcImg, Option option, IAnimateSubActor hook, JPanel pnl) {
		super(x, y, srcImg);
		this.move = new MoveItems(location);
		this.hook = hook;
		this.pnl = pnl;
		this.option = option;
	}

	@Override
	public void move() {
		// auto move thoy
		if (isMove) {
			if (hasOutPanel()) {
				reset();
			} else {
				if (isStandLeft) {
					move.moveRight();
				} else {
					move.moveLeft();
				}
				
				//ngu mot khoang thoi gian nho nho =))
				try {
					Thread.sleep(6);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			move.stop();
		}

	}

	public boolean hasOutPanel() {
		int min = this.pnl.getX() - this.width;
		int max = this.pnl.getX() + this.pnl.getWidth();

		if (location.getX() < min || location.getX() > max) {
			return true;
		}
		return false;
	}

	public void reset() {
		Location loc = randLocation();
		this.location.setX(loc.getX());
		this.location.setY(loc.getY());
		findOrientation(location.getX());
		System.out.println(location);
	}

	public boolean findOrientation(int x) {
		if (x < (pnl.getX() + (pnl.getWidth() / 2))) {
			isStandLeft = true;
		} else {
			isStandLeft = false;
		}
		changeImage();
		return isStandLeft;
	}

	private void changeImage() {
		if (isStandLeft) {
			img = loadImage.getbufferImage("fish1-right.png");
		} else {
			img = loadImage.getbufferImage("fish1-left.png");
		}
	}

	public Location randLocation() {
		Random rand = new Random();
		double m = Math.random();
		int x;
		if (m <= 0.5) {
			x = this.pnl.getX() - this.width; // 0 - 48 = -48
		} else {
			x = this.pnl.getX() + this.pnl.getWidth(); // 0 + 800 = 800
		}
//		int y = rand.nextInt(this.pnl.getY() + this.pnl.getHeight()/2);
//		y += this.pnl.getHeight()/2;
		int h = this.pnl.getHeight()/2;
		int y =  (int)(h  * Math.random() + h) - this.height;
		return new Location(x, y);
	}

	@Override
	public void show(Graphics g) {
		g.drawImage(img, location.getX(), location.getY(), null);
	}

	/**
	 * di chuyen theo toa do hook
	 */
	@Override
	public void updateLocation(Location loc) {
		// location cua fish = hook
		this.location.setX(loc.getX());
		this.location.setY(loc.getY());
	}

	/**
	 * kiem tra xem co bi thang hook cui bap bat chua ?
	 */
	@Override
	public void check() {
		checkIsCatched();
		System.out.println("fish is checking....");
		if (isCatched()) {
			catched();
		} else {
			System.out.println("hahaha minh chua bi bat!");
		}
	}
	
	public void checkIsCatched() {
		if (img != null) {
			System.out.println("width fish = " + width + " -- heght fish = "
					+ height);
			Rectangle rect = getBoundingBox();
			Rectangle recHook = hook.getBoundingBox();
			if (rect.intersects(recHook)) {
				isCatch = true;
			}
		} else {
			System.out.println("I'm not display");
		}
	}
	
	public void catched() {
		hook.catchedItems(this);
		isMove = false;
		System.out.println("I was be catched!");
	}

	public boolean isCatched() {
		return isCatch;
	}

	@Override
	public Location getLocation() {
		return location;
	}

	@Override
	public void addOptionForBoat(Option option) {
		// TODO Auto-generated method stub
		hook.addOptionForBoat(option);
	}

	@Override
	public void notifyObsever() {
		// nothing
	}

	@Override
	public void addObsever(IAnimateItems obsever) {
		// TODO Auto-generated method stub
		// nothing
	}

	@Override
	public void removeObsever(IAnimateItems obsever) {
		// TODO Auto-generated method stub
		// nothing
	}

	@Override
	public void destroyed() {
		addOptionForBoat(option);
		reset();
		isMove = true;
		isCatch = false;
	}

	@Override
	public Option getOption() {
		return option;
	}

	@Override
	public Rectangle getBoundingBox() {
		return location.getBounds(width, height);
	}

	@Override
	public void setMove(IMove move) {
		this.move = move;
	}

}
